package day9;
//通过synchronized 模拟多线程 轮流并发执行
//假设三个线程 A B C 要求顺序是ABC ABC ABC...
//2.借助于它的wait和notify实现
public class Demo6 implements Runnable{
    int id;
    int n=0;
    static int count;
    static int len=3;
    public Demo6(int id){
        this.id=id;
    }
    public void run() {
        String name= Thread.currentThread().getName();
        synchronized (Demo6.class){
            while (n<5){
                if(count%len==id){
                    //len一开始为3 0/3---0 id=0
                    //1/3---1 id=1
                    //2/3---2 id=2
                    System.out.print(name);
                    count++;//1
                    n++;//1
                    Demo6.class.notifyAll();
                }else{
                    try {
                        Demo6.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
    public static void main(String[] args) {
       // Demo6.len=3;
        for(int i=0;i<3;i++){
            //A B C
            new Thread(new Demo6(i),(char)(65+i)+"").start();
        }
        //new Thread(new Demo6(1),"B").start();
        //new Thread(new Demo6(2),"C").start();
    }
}
